/* eslint-disable max-len */
import { get, post } from '@/utils/fetch';

/**
 * 这两个API是两个暂时API，主要是暂时如何规范的写API参数和如何组合API
 */

// 公用的API可以提到这个位置
export const getData = data => get('/api/fetch', data);

export const getOrgList = data => post('/usercenter-admin-app/admin/sysOrg/selectPage', data);

export const getOrgRootTree = data => post('/usercenter-admin-app/admin/sysOrg/getFirstLevelOrgList', data);

export const getChildOrgListByParentId = data => post('/usercenter-admin-app/admin/sysOrg/getChildOrgListByParentId', data);

export const addOrg = data => post('/usercenter-admin-app/admin/sysOrg/insert', data);
export const updateOrg = data => post('/usercenter-admin-app/admin/sysOrg/update', data);
export const deleteOrg = data => post('/usercenter-admin-app/admin/sysOrg/delete', data);

export const getPositionList = data => post('/usercenter-admin-app/admin/sysPosition/selectPage', data);
export const getAllPositionList = data => post('/usercenter-admin-app/admin/sysPosition/qryAllPositions', data);
export const getListDistinctPositionNames = data => post('/usercenter-admin-app/admin/sysPosition/listDistinctPositionNames', data);
export const addPosition = data => post('/usercenter-admin-app/admin/sysPosition/insert', data);
export const delPosition = data => post('/usercenter-admin-app/admin/sysPosition/delete', data);
export const setMainPosition = data => post('/usercenter-admin-app/admin/sysPosition/update/main', data);
export const validateDuplicatedSort = sortNumber => get(`/usercenter-admin-app/admin/sysOrg/sort/${sortNumber}`);

/**
 * 假设这个后端请求,允许的参数是 {id}
 * 返回的数据结构是 [{...}, {...}, {...}]
 * 我们的业务是通过id获取其中的一个内容
 * 我们可以写成如下逻辑
 * @param id 请求ID
 * @returns
 */
export const getDataById = id =>
  getData({ id }).then(
    // 对得到的数据做一个预处理,提取列表中的第一个元素
    ({ data }) => data?.[0]
  );
